﻿using Masuit.Tools;
using Quartz;
using Quartz.Impl;
using SqlSugar;
using System.Threading.Tasks;
using ztDataNet.db;
namespace ztDataNet
{
   

    public class DataCheckJob : IJob
    {
        private readonly ISqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
        {
            ConnectionString = "Server=119.45.240.39;Port=3306;Database=ztdata;Uid=ztData;Pwd=bPXREBwmFcMLA3sG;", // 这里填写你的数据库连接字符串
            DbType = DbType.MySql, // 设置数据库类型
            IsAutoCloseConnection = true, // 自动释放数据务，如果存在事务，在事务结束后释放
            InitKeyType = InitKeyType.Attribute // 从实体特性中读取主键自增列信息
        });
        public async Task Execute(IJobExecutionContext context)
        {
            DateTime now = DateTime.Now;

            // 将时间格式化为 "yyyy-MM-dd" 格式的字符串
            string date = now.ToString("yyyy-MM-dd");
            string time = now.ToString("HH:mm");

           var l = db.Queryable<CalendarList>().Where(x=>x.date == date).Where(x=>x.time == time).ToList();
           // Serilog.Log.Information($"DataCheckJob INFO: {l.ToJsonString()}");
            foreach (CalendarList c in l)
            {
                var type = "备忘";
                if(c.avatar == "https://ztapp-1304966785.cos.ap-nanjing.myqcloud.com/%E4%BB%A3%E5%8A%9E.png")
                {
                    type ="代办";
                }
                utils.Send_Subscribe(c.openid,  c.title, c.details, type);
            }
            
        }

       
    }

    class Program
    {
        private static async Task Main(string[] args)
        {
            StdSchedulerFactory factory = new StdSchedulerFactory();
            IScheduler scheduler = await factory.GetScheduler();
            await scheduler.Start();

            IJobDetail job = JobBuilder.Create<DataCheckJob>()
                .WithIdentity("dataCheckJob", "group1")
                .Build();

            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("trigger1", "group1")
                .StartNow()
                .WithSimpleSchedule(x => x
                    .WithIntervalInMinutes(1)
                    .RepeatForever())
                .Build();

            await scheduler.ScheduleJob(job, trigger);
        }
    }

}
